package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.Banner;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtilsWx;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/8/14
 * @Time: 下午7:42
 */
public class BannerController extends BaseController {

    @Override
    public void list() {
        String sql = "from banner where 1 = 1 order by id desc";
        Page<Banner> data = Banner.dao.paginate(getPageIndex("banner"), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        render("banner_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        Banner banner = Banner.dao.findById(id);
        setAttr("banner", banner);
        render("banner_edit.html");
    }

    @Override
    public void save() {
        UploadFile uploadFile = getFile("banner.img");
        String filePath = "";
        if (uploadFile != null && !StringUtilsWx.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        Banner us = getModel(Banner.class, "banner");
        if (!StringUtilsWx.isEmpty(filePath)) {
            us.setImg(filePath);
        }
        if (us != null && us.getId() != null) {
            us.update();
        } else {
            us.save();
        }

        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from banner where id in (" + ids + ")");

        list();
    }
}
